<?php
/**
 * класс отвечает за преобразование массивов
 *
 */
class Service_Array{
	
	public $arrConvert = array();
	
	public function convertLinealToTree($arrFolders){
    $arrOut = array();
    $linkArray = array();
    do {
     $changed = false;
     $arrSize = sizeof($arrFolders);
     $unsortArray = array();
     for ($i = 0; $i < $arrSize; $i++) {
      $id = $arrFolders[$i]['ID_folder'];
       $idRoot = $arrFolders[$i]['ID_root_folder'];

       if ($idRoot == null) {
         $arrOut[] = $arrFolders[$i];
         $linkArray[$id] = &$arrOut[(sizeof($arrOut) -1)];
         $changed = true;
       }
       else{
         if(isset($linkArray[$idRoot])) {
           $linkArray[$idRoot]['children'][] = $arrFolders[$i];
           $linkArray[$id]= &$linkArray[$idRoot]['children'][(sizeof($linkArray[$idRoot]['children']) -1)];
           $changed = true;
         }
         else
           $unsortArray[] = $arrFolders[$i];
       }
     }
     $arrFolders = $unsortArray;
    } while(sizeof($unsortArray) && $changed);
    return $arrOut;
	}
}
?>